<template>
  <el-dialog :title="title" :visible.sync="dialogTableVisible" @close="closeDialog" @open="openDialog">
    <div style="height: 400px;overflow: scroll">
      <el-table :data="gridData">
        <el-table-column v-if="interfaceType == 1" label="学校"
                         property="xxName"></el-table-column>
        <el-table-column v-if="interfaceType == 1" label="完成数" property="total" width="80"></el-table-column>
        <el-table-column v-if="interfaceType == 2" label="学校" property="xx_name"></el-table-column>
        <el-table-column v-if="interfaceType == 2" label="关注人数" property="value" width="90"></el-table-column>
        <el-table-column v-if="interfaceType == 3" label="学校"
                         property="xxName"></el-table-column>
        <el-table-column v-if="interfaceType == 3" label="完成率(%)" property="rate" width="90"></el-table-column>

        <el-table-column v-if="interfaceType == 4" label="学校"
                         property="xxName"></el-table-column>
        <el-table-column v-if="interfaceType == 4" label="完成数" property="rate" width="90"></el-table-column>

        <el-table-column v-if="interfaceType == 5" label="学校"
                         property="xxName"></el-table-column>
        <el-table-column v-if="interfaceType == 5" label="完成率" property="rate" width="90"></el-table-column>

        <el-table-column v-if="interfaceType == 6" label="年级"
                         property="njName"></el-table-column>
        <el-table-column v-if="interfaceType == 6" label="完成数" property="total" width="90"></el-table-column>

        <el-table-column v-if="interfaceType == 7" label="年级"
                         property="njName"></el-table-column>
        <el-table-column v-if="interfaceType == 7" label="完成率" property="rate" width="90"></el-table-column>

        <el-table-column v-if="interfaceType == 8" label="班级"
                         property="bjName"></el-table-column>
        <el-table-column v-if="interfaceType == 8" label="完成数" property="total" width="90"></el-table-column>

        <el-table-column v-if="interfaceType == 9" label="班级"
                         property="bjName"></el-table-column>
        <el-table-column v-if="interfaceType == 9" label="完成率" property="rate" width="90"></el-table-column>

        <el-table-column label="排名" property="index" width="50"></el-table-column>
      </el-table>
      <el-pagination
        :total="total"
        class="pagination"
        layout="prev, pager, next"
        small
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </el-dialog>

</template>

<script>

import {getStore} from "@/util/store";
import {
  statisticAreaCountAndRatePage,
  statisticClassesCountAndRatePage,
  statisticGradeCountAndRatePage,
  statisticSchoolCountAndRatePage,
  statisticSchoolCountByAreaPage,
} from "@/api/platform/templateManage";

export default {
  props: {
    dialogTableVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: ''
    },
    studentLevel: {
      type: String,
      default: ''
    },
    dataType: {
      type: String,
      default: 'total'
    },
    filterCondition: {
      type: Object,
      default: () => {
        return {};
      }
    },
    /*
    * interfaceType 参数
    *  1、省、市、县各学校完成数
    *  2、省、市、县各学校完成率
    *  3、重点关注人数学下排名
    *  4、省市各区完成数排名
    *  5、省市各区完成率排名
    *  6、学校各年级完成数排名分页列表
    *  7、学校各年级完成率排名分页列表
    *  8、年级各班级完成数排名分页列表
    *  9、年级各班级完成率排名分页列表
    * */
    interfaceType: {
      type: String,
      default: '1'
    }
  },

  data() {
    return {
      isRanking: false,
      gridData: [],
      total: 0,
      current: {
        page: 1,//当前页
        size: 10,//每页条数
      }
    };
  },

  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.current.page = val
      this.openDialog()
      // console.log(`当前页: ${val}`);
    },
    openDialog() {
      // 0:省教育局， 1：市教育局，2：县教育局，3：校长 4：年级组长 5：班主任
      let newUserInfo = getStore({name: 'newUserInfo'})
      console.log('newUserInfo', newUserInfo)
      let params = {}
      if (this.type == 0) {
        params.provinceCode = newUserInfo.provinceCode
      } else if (this.type == 1) {
        params.provinceCode = newUserInfo.provinceCode
        params.cityCode = newUserInfo.cityCode
      } else if (this.type == 2) {
        params.provinceCode = newUserInfo.provinceCode
        params.cityCode = newUserInfo.cityCode
        params.areaCode = newUserInfo.areaCode
      } else if (this.type == 3) {
        params.provinceCode = newUserInfo.provinceCode
        params.cityCode = newUserInfo.cityCode
        params.areaCode = newUserInfo.areaCode
        params.xxId = newUserInfo.xxId
      } else if (this.type == 4) {
        params.provinceCode = newUserInfo.provinceCode
        params.cityCode = newUserInfo.cityCode
        params.areaCode = newUserInfo.areaCode
        params.xxId = newUserInfo.xxId
        params.njId = newUserInfo.njId
      } else if (this.type == 5) {
        params.provinceCode = newUserInfo.provinceCode
        params.cityCode = newUserInfo.cityCode
        params.areaCode = newUserInfo.areaCode
        params.xxId = newUserInfo.xxId
        params.njId = newUserInfo.njId
        params.bjId = newUserInfo.bjId
      }
      this.filterCondition.dataType = this.dataType
      params.filterCondition = this.filterCondition
      params.studentLevel = this.studentLevel
      params.page = this.current.page
      params.size = this.current.size
      console.log('this.interfaceType', this.interfaceType)
      if (this.interfaceType == '1' || this.interfaceType == '3') {
        statisticSchoolCountAndRatePage(params).then(res => {
          if (res.data.code === 200) {
            this.successCallback(res)
          }
        })
      } else if (this.interfaceType == '2') {
        statisticSchoolCountByAreaPage(params).then(res => {
          if (res.data.code === 200) {
            this.successCallback(res)
          }
        })
      } else if (this.interfaceType == '4' || this.interfaceType == '5') {
        statisticAreaCountAndRatePage(params).then(res => {
          if (res.data.code === 200) {
            this.successCallback(res)
          }
        })
      } else if (this.interfaceType == '6' || this.interfaceType == '7') {
        statisticGradeCountAndRatePage(params).then(res => {
          if (res.data.code === 200) {
            this.successCallback(res)
          }
        })
      } else if (this.interfaceType == '8' || this.interfaceType == '9') {
        statisticClassesCountAndRatePage(params).then(res => {
          if (res.data.code === 200) {
            this.successCallback(res)
          }
        })
      }
      console.log('打开弹窗')
    },
    // statisticSchoolCountByAreaPage
    closeDialog() {
      this.$emit('closeDialog')
      console.log('关闭弹窗')
    },
    successCallback(res) {
      // if (res.data.code === 200) {
      this.current.page = res.data.data.current
      this.total = res.data.data.total
      this.gridData = res.data.data.records
      res.data.data.records.forEach((item, index) => {
        item.index = ((this.current.page - 1) * 10) + index + 1
      })
      // this.isRanking = res.data.data.records.length > 0 ? true : false
      console.log('this.gridData', this.gridData)
      // }
    },
  }
};
</script>

<style>

.pagination {
  padding-top: 10px;
  text-align: right;
}
</style>
